package bdz.projects.lavender.opsm.ui {
	import bdz.common.events.GenericEvent;
	import flash.display.Sprite;
	import bdz.common.display.DisplayLoader;
	import flash.events.Event;
	import com.greensock.*;
	import com.greensock.easing.*;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author ...
	 */
	public class ProductImageList extends Sprite {
		private var _source:Array;
		private var _imageWidth:Number;
		private var _imageHeight:Number;
		private var _padding:Number = 0;
		private var _imageRef:Array;
		private var _firstImage:String;
		private var _icount:Number;
		private var _iloaded:Number = 0;
		function ProductImageList(arr:Array) {
			_source = arr.slice();

			_firstImage = _source.shift();
			
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			
			_imageRef = new Array();
			_icount = _source.length;
			
		}
		private function createLoader(xx:Number, yy:Number, src:String):void {
			var display:DisplayLoader = new DisplayLoader();
			display.smoothenBitmap = true;
			display.alpha = 0;
			display.addEventListener(Event.COMPLETE, onImageComplete);
			display.buttonMode = true;
			display.addEventListener(MouseEvent.CLICK, onImageClick);
			display.source = src;
			display.x = xx;
			display.y = yy;
			addChild(display);
		}
		private function onImageClick(evt:MouseEvent):void {
			
			dispatchEvent(new GenericEvent(GenericEvent.CLICKED, { targ:evt.target } ));
		}
		private function onImageComplete(evt:Event):void {
			
			evt.target.width = _imageWidth;
			evt.target.height = _imageHeight;
			TweenLite.to(evt.target, .8, { alpha:1, ease:Strong.easeInOut, onComplete:countLoaded } );
		}
		private function countLoaded():void {
			if (_iloaded < _icount-1) {
				_iloaded++;
			}else {
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		public function loadThumbs():void {
			var sx:Number = 0;
			var sy:Number = 0;
			for each(var str:String in _source) {
				createLoader(sx, sy, str);
				sx += _imageWidth + _padding;
			}
		}
		public function set imageWidth(n:Number):void {
			_imageWidth = n;
		}
		public function get imageWidth():Number {
			return _imageWidth;
		}
		public function set imageHeight(n:Number):void {
			_imageHeight = n;
		}
		public function get imageHeight():Number {
			return _imageHeight;
		}
	}
	
	
}